var express = require('express');
const myroute = express.Router()
const mySql = require('../model/index')

//信息中心渲染
myroute.get('/getNew',function(req,res){
  let sql=`SELECT * FROM notice  JOIN 
  notice_class ON  notice.notice_class=notice_class.notice_classId 
  JOIN notice_state ON notice.notice_state=notice_state.notice_stateId ORDER BY notice_id `;
  mySql.query(sql,function(err,data){
    if(err){
      res.send({code:1,err:err})
    }else{
      res.send({code:200,data:data})
    }
  })
})
// 信息中心查询
myroute.post('/postNew',(req,res)=>{
  // console.log(req.body.data);
  let sql=`SELECT * FROM 
  (SELECT * FROM notice   JOIN notice_class ON notice.notice_class=notice_class.notice_classId
   JOIN notice_state ON notice.notice_state=notice_state.notice_stateId ORDER BY notice_id ) AS NEW
   WHERE  notice_className like "%${req.body.data.class}%" AND notice_title LIKE "%${req.body.data.title}%" 
   AND notice_author LIKE  "%${req.body.data.author}%" AND  notice_stateName  LIKE  "%${req.body.data.state}%"AND  
   notice_time>="${req.body.data.time[0] || '1900-1-1'}" and notice_time<="${req.body.data.time[1] || '2999-1-1'}" ` 
   mySql.query(sql,(err,data)=>{
    if(err){
      res.send({code:1,err:err})
    }else{
      res.send({code:200,data:data})
    }
   })
})
// 信息中心添加
myroute.post('/postAdd',(req,res)=>{
  // console.log(req.body.data.data,req.body.data.editor);
  let time=req.body.data.data.date1.substring(0,10)
  // console.log(time);
  let sql=`INSERT INTO notice  (notice_id,notice_author,notice_time,notice_content,notice_title,digest,
    top,tag,notice_class,notice_state) VALUES(null,"${req.body.data.data.author}","${time}",
    "${req.body.data.editor}","${req.body.data.data.title}","${req.body.data.data.digest}","${req.body.data.data.top}",
    "${req.body.data.data.tag}","${req.body.data.data.class}","${req.body.data.data.state}") `
  mySql.query(sql,(err,data)=>{
    if(err){
      res.send({code:1,err:err})
    }else{
      res.send({code:200,msg:"添加成功"})
    }
  })
})
//信息中心编辑修改
myroute.post('/postEdit',(req,res)=>{
  // console.log(req.body.data);
  let classId= req.body.data.class
 if(classId=="公告通知"){
     classId="1"
 }else if(classId=="公司新闻"){
      classId="2"
 }else if(classId=="规章制度"){
  classId="3"
}else if(classId=="办事指南"){
  classId="4"
}else if(classId=="下载中心"){
  classId="5"
}
// console.log(classId);     
let sql=`UPDATE notice SET  notice_time='${req.body.data.date1}',notice_content='${req.body.data.content}',
notice_class=${Number(classId)},notice_state=${req.body.data.state},notice_title='${req.body.data.title}',
digest='${req.body.data.digest}',top='${req.body.data.top}',tag='${req.body.data.tag}' WHERE notice_id=${req.body.data.id}`
mySql.query(sql,(err,data)=>{
  if(err){
    res.send({code:1,err:err})
  }else{
    res.send({code:200,msg:"修改成功"})
  }
})
})
// 信息中心删除
myroute.post('/postDel',(req,res)=>{
// console.log(req.body.id);
let sql=`DELETE FROM notice WHERE notice_id=${req.body.id}`
mySql.query(sql,(err,data)=>{
  if(err){
    res.send({code:1,err:err})
  }else{
    res.send({code:200,msg:"删除成功"})
  }
})
})



module.exports = myroute